<?php


namespace app\controller\api;


use app\BaseController;
use think\facade\Db;

class SmsController extends BaseController
{
    public function sendSms()
    {
        try {
            $sign = $this->request->param('sign', '', 'trim');
            if (!$sign) {
                throw new \Exception('非法请求');
            }
            if ($sign != 'bljlkj2023@@..') {
                throw new \Exception('签名错误');
            }
            $appId = $this->request->param('app_id', '', 'trim');
            if (!$appId) {
                throw new \Exception('缺少参数APPID');
            }
            $info = Db::name('sms')->where('app_id', $appId)->find();
            if (!$info) {
                throw new \Exception('短信配置不存在');
            }
//            $context = $this->request->param('context', '', 'trim');
            $count = $this->request->param('count', '', 'trim');
            Db::name('sms')->where('app_id', $appId)->update([
                'num' => $info['num'] - $count,
                'total_use_num' => $info['total_use_num'] + $count,
                'update_time' => time()
            ]);
//            Db::name('sms_record')->insert([
//                'sms_id' => $info['id'],
//                'context' => $context,
//                'create_time' => time(),
//                'update_time' => time(),
//            ]);
            return $this->success();
        } catch (\Exception $exception) {
            file_put_contents(date('m-d') . '短信发送失败.txt', $exception->getMessage() . PHP_EOL, FILE_APPEND);
            return $this->error($exception->getMessage());
        }
    }
}